package com.yutianran.ezadapter;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class TypeHelper {

    public static <T> Class getActualTypeArguments(LayoutPlayer<T> player) {
        Type[] interfaces = player.getClass().getGenericInterfaces();
        for (Type type : interfaces) {
            if (type instanceof ParameterizedType) {
                if (((ParameterizedType) type).getRawType().equals(LayoutPlayer.class)) {
                    Type actualType = ((ParameterizedType) type).getActualTypeArguments()[0];
                    if (actualType instanceof Class) {
                        return (Class) actualType;
                    } else {
                        throw new IllegalArgumentException("泛型参数异常");
                    }
                }
            }
        }
        return null;
    }
}
